#include "mavlink_server.h"
#include <iostream>

MavlinkServer::MavlinkServer(QObject *parent)
    : QObject(parent) {  // 可修改 "0.0.0.0" 为具体IP
      
}
MavlinkServer::~MavlinkServer() {
    running_ = false;
    if (thread_receive_->joinable()) {
        thread_receive_->join();
    }
}

void MavlinkServer::start(const std::string& ip, uint16_t port) {
    running_ = true;
    tcp_server_ = std::make_shared<TCPServer>(ip, port);
    // thread_receive_ = std::make_shared<std::thread>(&MavlinkServer::receive_thread, this);
    tcp_server_->start();
    std::cout << "MavlinkServer start" << std::endl;
}

void MavlinkServer::set_mavlink_message_callback(TCPServer::MavlinkMessageCallback callback) {
    tcp_server_->set_mavlink_message_callback(callback);
}

void MavlinkServer::send_mavlink_message(const mavlink_message_t& message) {
    tcp_server_->send_mavlink_message(message);
}

void MavlinkServer::receive_thread() {
    while(!running_) {
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    // io_context_->run();
}